package com.sihan.swvp.action.sys;




import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.sys.Gsgg;
import com.sihan.swvp.service.sys.GsggService;
import com.sihan.swvp.service.sys.LogService;


@Controller
@RequestMapping("/GsggAction")
public class GsggAction extends BaseAction{
	
	@Autowired
	private GsggService gsggService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping(value="/getGsgg")
	public Object getGsgg(Gsgg bean,@Param("page")Integer page,@Param("limit")Integer limit){
		return pageData(gsggService.getGsgg(bean,page,limit));
	} 
	
	
	@RequestMapping(value="/addGsgg")
	public void addGsgg(@RequestParam("file")MultipartFile file,@RequestParam("title")String title, 
			@RequestParam("nr")String nr,HttpServletResponse response){
		ServletOutputStream output = null;
		Object msg = successMsg();
		try {
			response.setContentType("text/html;charset=UTF-8");
			output = response.getOutputStream();
			String name = file.getOriginalFilename();
			String fileType = name.substring(name.lastIndexOf(".")+1, name.length());
			if(!"jpgpngjpeg".contains(fileType)){
				msg = failMsg("请上传jpg、png、jpeg格式的图片！");
				return;
			}
			if(file.getSize() > 1048576){
				msg = failMsg("图片文件超过1M，请重新选择！");
				return;
			}
			Gsgg bean=new Gsgg();
			bean.setTitle(title);
			bean.setNr(nr);
			bean.setUpuser(getLoginUser().getId());
			System.out.println(title);
			bean.setImage(file.getBytes());
			gsggService.addGsgg(bean);
			logService.addUserLog("添加公告", "公告", getLoginUser().getId(), "添加公告："+title,getRemoteAddr(),1,1);
		} catch (Exception e) {
			msg = failMsg("服务器异常");
			logService.addUserLog("添加公告", "公告", getLoginUser().getId(), "添加公告："+title,getRemoteAddr(),0,1);
		}
		finally{
			try {
				output.write(JSON.toJSONString(msg).getBytes("utf-8"));
				output.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
			CommonUtil.close(output);
		}
	} 

	@RequestMapping(value="/updateGsgg")
	public void updateGsgg(@RequestParam("id")Integer id,@RequestParam("title")String title,
			@RequestParam("nr")String nr,@RequestParam("file")MultipartFile file,HttpServletResponse response){
		ServletOutputStream output = null;
		Object msg = successMsg();
		try {
			response.setContentType("text/html;charset=UTF-8");
			output = response.getOutputStream();
			String name = file.getOriginalFilename();
			String fileType = name.substring(name.lastIndexOf(".")+1, name.length());
			if(!"jpgpngjpeg".contains(fileType)){
				msg = failMsg("请上传jpg、png、jpeg格式的图片！");
				return;
			}
			if(file.getSize() > 1048576){
				msg = failMsg("图片文件超过1M，请重新选择！");
				return;
			}
			Gsgg bean=new Gsgg();
			bean.setId(id);
			bean.setTitle(title);
			bean.setNr(nr);
			bean.setUpuser(getLoginUser().getId());
			if(file.getBytes().length !=0){
				bean.setImage(file.getBytes());
			}
			gsggService.updateGsgg(bean);
			logService.addUserLog("修改公告", "公告", getLoginUser().getId(), "修改公告："+title,getRemoteAddr(),1,1);
		} catch (Exception e) {
			msg = failMsg("服务器异常");
			logService.addUserLog("修改公告", "公告", getLoginUser().getId(), "修改公告："+title,getRemoteAddr(),0,1);
		}
		finally{
			try {
				output.write(JSON.toJSONString(msg).getBytes("utf-8"));
				output.flush();
			} catch (IOException e) {
				e.printStackTrace();
			}
		    
			CommonUtil.close(output);
		}
	} 
	
	@ResponseBody
	@RequestMapping(value="/deleteGsgg")
	public Object delGsgg(@Param("id")int id,String title){
		try {
			gsggService.deleteGsgg(id);
			logService.addUserLog("删除公告", "公告", getLoginUser().getId(), "删除公告："+title,getRemoteAddr(),1,1);
		} catch (Exception e) {
			failMsg("服务器异常");
			logService.addUserLog("删除公告", "公告", getLoginUser().getId(), "删除公告："+title,getRemoteAddr(),0,1);
		}
		return successMsg();
	} 
	
	@ResponseBody
	@RequestMapping(value="/getGsggList")
	public Object getGsggList(Gsgg bean,@Param("page")Integer page,@Param("limit")Integer limit){
		return gsggService.getGsgg(bean,1,8);
	}
	
	@ResponseBody
	@RequestMapping(value="/getGsggImg")
	public void getGsggImg(Gsgg bean,HttpServletResponse response){
		ServletOutputStream output = null;
		FileInputStream in = null;
		try {
			output = response.getOutputStream();
			Map<?,?> data = gsggService.getGsggImg(bean);
			byte[] bytes = (byte[]) data.get("image");
			output.write(bytes);
		    output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally{
			CommonUtil.close(output);
			CommonUtil.close(in);
		}
	} 
	
	
	
	
	/**
	 * 获取最新五条图片公告
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/getImgs")
	public Object getImgs(){
		List<Map<String,Object>> data = gsggService.getImgs();
		
		return data;
	}
	
	@ResponseBody
	@RequestMapping(value="/getImg")
	public void getImg(HttpServletResponse response,int id){
		ServletOutputStream output = null;
		FileInputStream in = null;
		try {
			output = response.getOutputStream();
			Gsgg gsgg = gsggService.getGsggById(id);
			byte[] bytes = (byte[]) gsgg.getImage();
			output.write(bytes);
		    output.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally{
			CommonUtil.close(output);
			CommonUtil.close(in);
		}
	} 
}
